package com.leyou.item.controller;

import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.SpecGroup;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.service.SpecGroupService;
import com.leyou.item.service.SpecParamService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/spec")
public class SpecController {
    @Resource
    private SpecGroupService groupService;
    @Resource
    private SpecParamService paramService;
    @GetMapping("/groups/of/category")
    public ResponseEntity<List<SpecGroupDTO>> queryGroupByCategory(@RequestParam("id")Long id){
        return ResponseEntity.ok(groupService.querySpecGroupByCategoryId(id));
    }
    @GetMapping("/params")
    public ResponseEntity<List<SpecParamDTO>> querySpecParams(
            @RequestParam(value = "categoryId",required = false)Long categoryId,
            @RequestParam(value = "groupId",required = false)Long groupId,
            @RequestParam(value = "searching",required = false)Boolean searching
    ){
        return ResponseEntity.ok(paramService.queryParams(categoryId,groupId,searching));
    }

    @PostMapping("/group")
    public ResponseEntity<Void> saveGroup(@RequestBody SpecGroupDTO specGroupDTO){
        groupService.save(specGroupDTO.toEntity(SpecGroup.class));
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @PutMapping("/group")
    public ResponseEntity<Void> updateGroup(@RequestBody SpecGroupDTO specGroupDTO){
        groupService.updateGroup(specGroupDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    @PostMapping("/param")
    public ResponseEntity<Void> saveParam(@RequestBody SpecParamDTO specParamDTO){
        paramService.save(specParamDTO.toEntity(SpecParam.class));
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @PutMapping("/param")
    public ResponseEntity<Void> updateParam(@RequestBody SpecParamDTO specParamDTO){
        paramService.updateParam(specParamDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    @GetMapping("/list")
    public ResponseEntity<List<SpecGroupDTO>> querySpecList(@RequestParam("id") Long categoryId) {
        return ResponseEntity.ok(groupService.querySpecList(categoryId));
    }


}
